package com.firebird.app.fekpsl.adapter.exam

import android.text.Html
import android.text.Html.FROM_HTML_MODE_LEGACY
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.firebird.app.fekpsl.R
import com.firebird.app.fekpsl.bean.QuestionTestContentBean
import com.firebird.app.fekpsl.bean.TrueFalseQuestion
import com.google.gson.Gson

/**
 * Created by firebird on 2018/4/22.
 */
class TrueFalseQuestionAdapter : BaseQuickAdapter<TrueFalseQuestion, BaseViewHolder>(R.layout.exam_item_true_false_question) {
    inline fun <reified T> Gson.fromJson(json: String) = fromJson(json, T::class.java)
    override fun convert(helper: BaseViewHolder?, item: TrueFalseQuestion?) {
        helper ?: return
        item ?: return
        val index=helper.layoutPosition
        /*
        {"titleContent":"演唱歌曲的时候应该面带微笑，不做小动作。","analyze":"a","questionItemObjects":
            [{"prefix":"A","content":"是","score":null},{"prefix":"B","content":"否","score":null}],"correct":"A"}
         */
        val QuestionTestContent=item.content
        val bean = Gson().fromJson<QuestionTestContentBean>(QuestionTestContent)
        val titleContent=bean.titleContent
        helper.setText(R.id.tv_true_false_question_title, Html.fromHtml(titleContent,FROM_HTML_MODE_LEGACY))
        bean.questionItemObjects.forEachIndexed{ index, s ->
            val answer_choice=bean.questionItemObjects.get(index).prefix + "." + bean.questionItemObjects.get(index).content
            when(index) {
                0-> {
                    helper.setText(R.id.rb_true_false_answer_a, answer_choice)
                }
                1-> {
                    helper.setText(R.id.rb_true_false_answer_b, answer_choice)
                }
            }
        }
        helper.setText(R.id.tv_true_false_question_no,(index+1).toString())
    }
}
